//
//  XGRecommendTableViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/17.
//  Copyright © 2019 itcast. All rights reserved.
//

#import "XGUserListTableViewController.h"
#import "XGGuestViewController.h"
#import "XGHomeViewController.h"

#import "XGUserListTableViewCell.h"

/// cell重用标识符
static NSString *const XGUserListTableViewCellRueseIdentifier = @"XGUserListTableViewCell";

@implementation XGUserListTableViewController
{
    /// 用户数据模型
    AVUser *_user;
    /// 列表类型
    XGUserListType _type;
    /// 用户列表
    NSArray<AVUser *> *_userList;
}

#pragma mark - 构造方法

- (instancetype)initWithUserInfo:(AVUser *)user listType:(XGUserListType)type
{
    if (self = [super initWithNibName:nil bundle:nil]) {
        _user = user;
        _type = type;
    }
    
    return self;
}

+ (instancetype)viewControllerWithUserInfo:(AVUser *)user listType:(XGUserListType)type
{
    return [[self alloc] initWithUserInfo:user listType:type];
}

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBackAction)];

    [self setUpTableView];
    
    [self loadUserList];
}

#pragma mark - 事件监听

- (void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 数据源和代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _userList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XGUserListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XGUserListTableViewCellRueseIdentifier];
    cell.user = _userList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL isCurrentUser = [_userList[indexPath.row] isEqual:[XGAccountViewModel currentUser]];
    UIViewController *viewController = isCurrentUser ? [[XGHomeViewController alloc] init] : [XGGuestViewController viewControllerWithUserInfo:_userList[indexPath.row]];
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - 其他方法

/// 设置tableView
- (void)setUpTableView
{
    // 注册cell
    [self.tableView registerClass:[XGUserListTableViewCell class] forCellReuseIdentifier:XGUserListTableViewCellRueseIdentifier];
    
    // 设置行高
    self.tableView.rowHeight = 80;
}

/// 加载推荐用户列表
- (void)loadUserList
{
    NSString *title;
    switch (_type) {
        case XGUserListTypeRecommend:
            [self loadRecommendUserList];
            title = @"推荐用户";
            break;
        case XGUserListTypeFollower:
            title = @"粉丝";
            [self loadFollowerList];
            break;
        case XGUserListTypeFollowee:
            title = @"关注";
            [self loadFolloweeList];
            break;
    }
    
    self.navigationItem.title = title;
}

/// 加载推荐用户列表
- (void)loadRecommendUserList
{
    AVQuery *query = [[AVQuery alloc] initWithClassName:@"_User"];
    [query whereKey:@"objectId" notEqualTo: _user.objectId];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (error != nil || objects == nil) {
            DEBUG_Log(@"获取推荐用户列表失败");
            return;
        }
        
        self->_userList = objects;
        [self.tableView reloadData];
    }];
}

/// 加载粉丝列表
- (void)loadFollowerList
{
    [_user getFollowers:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        self->_userList = objects;
        [self.tableView reloadData];
    }];
}

/// 加载关注列表
- (void)loadFolloweeList
{
    [_user getFollowees:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        self->_userList = objects;
        [self.tableView reloadData];
    }];
}

@end
